import Foundation
import UIKit

public enum UIViewGradientDirection {
    case horizontally
    case vertically
}

public extension UIView {
   
    func addGradientLayer(gradientColors: [UIColor],gradientDirection direction: UIViewGradientDirection, gradientFrame: CGRect? = nil, startPoint: CGPoint = CGPoint(x: 0, y: 0.5), endPoint: CGPoint = CGPoint(x: 1, y: 0.5)) {
        
        //创建并实例化CAGradientLayer
        let gradientLayer: CAGradientLayer = CAGradientLayer()
        //设置frame和插入view的layer
        if let gradientFrame = gradientFrame {
            gradientLayer.frame = gradientFrame
        }else {
            gradientLayer.frame = CGRect.init(x: 0, y: 0, width: width, height: height)
        }
        
        gradientLayer.colors = gradientColors.map({ (color) -> CGColor in
            
            return color.cgColor
        })
        //(这里的起始和终止位置就是按照坐标系,四个角分别是左上(0,0),左下(0,1),右上(1,0),右下(1,1))
        if direction == .horizontally {
            
            gradientLayer.startPoint = CGPoint.init(x: 0, y: 0.5)
            gradientLayer.endPoint = CGPoint.init(x: 1, y: 0.5)
        }else {
            
            gradientLayer.startPoint = CGPoint.init(x: 0, y: 0)
            gradientLayer.endPoint = CGPoint.init(x: 0, y: 1)
        }
        
        self.layer.insertSublayer(gradientLayer, at: 0)
    }
    func addGradientLayerVertically(gradientColors: [UIColor], gradientFrame: CGRect? = nil, y: CGFloat = 0.3) {
        
        //创建并实例化CAGradientLayer
        let gradientLayer: CAGradientLayer = CAGradientLayer()
        //设置frame和插入view的layer
        if let gradientFrame = gradientFrame {
            gradientLayer.frame = gradientFrame
        }else {
            gradientLayer.frame = CGRect.init(x: 0, y: 0, width: width, height: height)
        }
        
        gradientLayer.colors = gradientColors.map({ (color) -> CGColor in
            
            return color.cgColor
        })
            
        gradientLayer.startPoint = CGPoint.init(x: 0, y: 0)
        gradientLayer.endPoint = CGPoint.init(x: 0, y: y)
        
        self.layer.insertSublayer(gradientLayer, at: 0)
    }
    
    /// 设置圆角
    @discardableResult
    func id_border(_ borderWidth: CGFloat?, _ borderColor:UIColor?=nil, _ cornerRadius:CGFloat?) -> UIView{
        self.layer.borderWidth = borderWidth ?? 0
        self.layer.borderColor = borderColor?.cgColor
        self.layer.cornerRadius = cornerRadius ?? 0
        self.layer.masksToBounds = true
        return self
    }
    
    /// 设置指定部位的圆角
    @discardableResult
    func id_borderSpecified(_ specified: UIRectCorner,cornerRadius:CGFloat) -> UIView {
        let maskPath = UIBezierPath.init(roundedRect: self.bounds, byRoundingCorners: specified, cornerRadii: CGSize(width: cornerRadius, height: cornerRadius))
        let maskLayer = CAShapeLayer()
        maskLayer.frame = self.bounds
        maskLayer.path = maskPath.cgPath
        self.layer.mask = maskLayer
        return self
    }
    
    /// x origin of view.
    var x: CGFloat {
        get {
            return frame.origin.x
        }
        set {
            frame.origin.x = newValue
        }
    }
    
    /// y origin of view.
    var y: CGFloat {
        get {
            return frame.origin.y
        }
        set {
            frame.origin.y = newValue
        }
    }
    
    /// Width of view.
    var width: CGFloat {
        get {
            return frame.size.width
        }
        set {
            frame.size.width = newValue
        }
    }
    
    /// Height of view.
    var height: CGFloat {
        get {
            return frame.size.height
        }
        set {
            frame.size.height = newValue
        }
    }
    
    /// Size of view.
    var size: CGSize {
        get {
            return frame.size
        }
        set {
            frame.size = newValue
        }
    }
    
    /// Origin of view.
    var origin: CGPoint {
        get {
            return frame.origin
        }
        set {
            frame.origin = newValue
        }
    }
    
    /// CenterX of view.
    var centerX: CGFloat {
        get {
            return center.x
        }
        set {
            center.x = newValue
        }
    }
    
    /// CenterY of view.
    var centerY: CGFloat {
        get {
            return center.y
        }
        set {
            center.y = newValue
        }
    }
    
    func superViewConreoller() -> UIViewController? {
        
        for view in sequence(first: self.superview, next: {$0?.superview}){

            if let responder = view?.next{
                if responder.isKind(of: UIViewController.self){
                    return responder as? UIViewController
                }
            }
        }
        return nil
    }
}

extension UIView {
    
    func showMessage(message: String) {
        showMessage(message: message.localized, afterDelay: CGFloat.app_hubInterval)
    }
    func showMessage(message: String, afterDelay: TimeInterval = CGFloat.app_hubInterval) {
        CommonToast.shared.showToast(message.localized, duration: afterDelay)
    }
    
    func showActivityView(_ text: String) -> MBProgressHUD {
        let hud = MBProgressHUD.showAdded(to: self, animated: true)
        hud.label.text = "\(text)..."
        return hud
    }
    
    /// 绘制虚线
    func addDashedLine(color: UIColor, dashedLineWidth: Int, lineHeight: CGFloat) {
        let path = UIBezierPath()
        path.move(to: CGPoint(x: 0, y: bounds.size.height / 2))
        path.addLine(to: CGPoint(x: self.bounds.size.width, y: bounds.size.height / 2))

        let dashedLayer = CAShapeLayer()
        dashedLayer.strokeColor = color.cgColor
        dashedLayer.lineWidth = lineHeight
        dashedLayer.lineJoin = .round
        dashedLayer.lineDashPattern = [NSNumber(value: dashedLineWidth), 3]

        dashedLayer.path = path.cgPath
        self.layer.addSublayer(dashedLayer)
    }
    
}

